package com.tools.web.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.tools.common.io.CharsetEnum;
import com.tools.common.object.Note;

import java.util.Map;
import java.util.Objects;

/**
 * 微信登录的处理器
 * */
@Note("微信登录的处理器")
public final class WxLoginHandler {

    @Note("微信小程序服务对象")
    private final WxMaService wxMaService;

    @Note("微信 API 必须的 appId")
    private final String appId;

    @Note("微信 API 必须的密钥")
    private final String secret;


    public WxLoginHandler(WxSource source) {
        this.wxMaService = WxKit.newWxMaServiceImpl(source);
        this.appId = source.getAppId();
        this.secret = source.getSecret();
    }

    public WxLoginHandler(WxSource source, WxMaService wxMaService) {
        if(wxMaService == null) throw new NullPointerException("小程序服务对象 WxMaService 为 null");
        this.wxMaService = wxMaService;
        this.appId = source.getAppId();
        this.secret = source.getSecret();
    }

    public WxMaService getWxMaService() {
        return wxMaService;
    }

    public String getAppId() {
        return appId;
    }

    public String getSecret() {
        return secret;
    }

    public WxMaJscode2SessionResult miniAppLogin(String code) {
         return WxKit.miniAppLogin(this.wxMaService, code);
    }

    public WxMaUserInfo getUserInfoForMiniApp(String sessionKey, String encryptData, String iv) {
        return WxKit.getWxMiniAppUserInfo(this.wxMaService, sessionKey, encryptData, iv);
    }

    public WxMaPhoneNumberInfo getPhoneNumberForMiniApp(String encryptData) {
        return WxKit.getWxPhoneNumberInfo(this.wxMaService, encryptData);
    }

    public String newQRLoginURL(String state, String redirectURL) {
        return WxKit.newQRLoginURL(this.appId, state, redirectURL, CharsetEnum.UTF_8);
    }

    public String newQRLoginURL(String state, String redirectURL, CharsetEnum charset) {
        return WxKit.newQRLoginURL(this.appId, state, redirectURL, charset);
    }

    public WxKit.WxLogin getWxLoginAccessToken(String code) {
        return WxKit.getWxLoginAccessToken(code, this.appId, this.secret);
    }

    public Map<String, Object> getWxUserInfo(String accessToken, String openId) {
        return WxKit.getWxUserInfo(accessToken, openId);
    }

    public Map<String, Object> loginForWeb(String code, String appId, String secret) {
        return WxKit.loginForWeb(code, appId, secret);
    }


    @Override
    public String toString() {
        return "WxLoginHandler{" +
                "wxMaService=" + wxMaService +
                ", appId=" + appId +
                ", secret=" + secret +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        WxLoginHandler that = (WxLoginHandler) o;
        return Objects.equals(appId, that.appId) && Objects.equals(secret, that.secret);
    }

    @Override
    public int hashCode() {
        return Objects.hash(appId, secret);
    }
}
